import Vue from 'vue'
import Router from 'vue-router'

// 解决重复router会报错的问题
// https://www.cnblogs.com/rever/p/11577322.html
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}


// 登录
const Login = () => import('./views/Login.vue')


// Intro
const dashboard = () => import('./views/dashboard/Index.vue')
// Basic
const authorityIndex = () => import('./views/authority/Index.vue')
const department = () => import('./views/authority/Department.vue')
const role      = () => import('./views/authority/Role.vue')
const user     = () => import('./views/authority/User.vue')

// 会议
const meeting = () => import('./views/meeting/List.vue')
const meetingDetail = () => import('./views/meeting/Detail.vue')

// 首页管理
const homeIndex = () => import('./views/home/Index.vue')
const news      = () => import('./views/home/News.vue')
const swiper    = () => import('./views/home/Swiper.vue')

// // Table
// const table = () => import('./views/table/Index.vue')

// // Form
// const form = () => import('./views/form/Index.vue')

// // Ability
// const abilityIndex   = () => import('./views/ability/Index.vue')
// const keepAlive      = () => import('./views/ability/KeepAlive.vue')
// const keepAliveInner = () => import('./views/ability/KeepAliveInner.vue')
// const authority      = () => import('./views/ability/Authority.vue')
// const globalData     = () => import('./views/ability/GlobalData.vue')

// // Components
// const components = () => import('./views/components/Index.vue')

// // Example
// const exampleIndex = () => import('./views/example/Index.vue')
// const list         = () => import('./views/example/List.vue')
// const detail       = () => import('./views/example/Detail.vue')
// const charts       = () => import('./views/example/Charts.vue')


Vue.use(Router)

// router配置说明
// 含有children的路由 需要配置一个Index.vue 用于加载子页面
// name               路由元名
// meta               附带属性
// meta.routerName    路由显示的名称
// meta.showNavLayout 当前页是否需要渲染导航
// meta.hidden        当前页是否要在导航中隐藏(详情页需要隐藏)
// meta.redirectName  在导航中隐藏的页面重新定位导航的名称(详情页需要配置此参数, 否则导航不会重新定位)
// meta.detail        标明当前页面是属于详情页 , 顶部的返回按钮将会显示

// 特别说明
// 当单个router页出现详情页的情况时 , 处理方法与普通多级router处理方式一致 , 但不再在Index中渲染routerView
export default new Router({
  mode: "history",
  scrollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return {
        // 通过 to.hash 的值來找到对应的元素
        selector: to.hash
      }
    }
  },
  routes: [
    {
      path: '/dashboard',
      name: 'dashboard',
      component: dashboard,
      meta: {
        routerName: '工作台',
        icon: 'el-icon-guide',
        showNavLayout: true
      },
      children: []
    },
    {
      path: '/authority',
      name: 'authority',
      component: authorityIndex,
      meta: {
        routerName: '权限管理',
        icon: 'el-icon-view',
        showNavLayout: true
      },
      children: [
        {
          path: 'department',
          name: 'department',
          component: department,
          meta: {
            routerName: '部门管理',
            showNavLayout: true,
          }
        },
        {
          path: 'role',
          name: 'role',
          component: role,
          meta: {
            routerName: '角色管理',
            showNavLayout: true,
          }
        },
        {
          path: 'user',
          name: 'user',
          component: user,
          meta: {
            routerName: '人员管理',
            showNavLayout: true,
          }
        },
      ]
    },
    {
      path: '/meeting',
      name: 'meeting',
      component: meeting,
      meta: {
        routerName: '会议管理',
        icon: 'el-icon-view',
        showNavLayout: true
      },
      children: []
    },
    {
      path: '/meetingDetail',
      name: 'meetingDetail',
      component: meetingDetail,
      meta: {
        routerName: '会议详情',
        icon: 'el-icon-guide',
        showNavLayout: true,
        hidden: true,
        redirectName: 'meeting'
      }
    },
    {
      path: '/home',
      name: 'home',
      component: homeIndex,
      meta: {
        routerName: '首页管理',
        icon: 'el-icon-view',
        showNavLayout: true
      },
      children: [
        {
          path: 'news',
          name: 'news',
          component: news,
          meta: {
            routerName: '要闻管理',
            showNavLayout: true,
          }
        },
        {
          path: 'swiper',
          name: 'swiper',
          component: swiper,
          meta: {
            routerName: '轮播管理',
            showNavLayout: true,
          }
        },
      ]
    },
    // {
    //   path: '/table',
    //   name: 'table',
    //   component: table,
    //   meta: {
    //     routerName: '表格 Table',
    //     icon: 'el-icon-notebook-2',
    //     showNavLayout: true
    //   },
    //   children: []
    // },
    // {
    //   path: '/form',
    //   name: 'form',
    //   component: form,
    //   meta: {
    //     routerName: '表单 Form',
    //     icon: 'el-icon-document-copy',
    //     showNavLayout: true
    //   },
    //   children: []
    // },
    // {
    //   path: '/ability',
    //   name: 'ability',
    //   component: abilityIndex,
    //   redirect: { name: 'keepAlive' },
    //   meta: {
    //     routerName: '功能 Ability',
    //     icon: 'el-icon-files',
    //     showNavLayout: true
    //   },
    //   children: [
    //     {
    //       path: 'keepAlive',
    //       name: 'keepAlive',
    //       component: keepAlive,
    //       meta: {
    //         routerName: '保持 keepAlive',
    //         keepAlive: true,
    //         showNavLayout: true
    //       },
    //     },
    //     {
    //       path: 'keepAliveInner',
    //       name: 'keepAliveInner',
    //       component: keepAliveInner,
    //       meta: {
    //         hidden: true,
    //         keepAlive: true,
    //         showNavLayout: true,
    //         redirectName:  'keepAlive',
    //         detail: true,
    //       },
    //     },
    //     {
    //       path: 'authority',
    //       name: 'authority',
    //       component: authority,
    //       meta: {
    //         routerName: '权限 Authority',
    //         showNavLayout: true,
    //       },
    //     },
    //     {
    //       path: 'globalData',
    //       name: 'globalData',
    //       component: globalData,
    //       meta: {
    //         routerName: '全局 GlobalData',
    //         showNavLayout: true,
    //       },
    //     }
    //   ]
    // },
    // {
    //   path: '/components',
    //   name: 'components',
    //   component: components,
    //   meta: {
    //     routerName: '组件 Components',
    //     icon: 'el-icon-box',
    //     showNavLayout: true
    //   },
    //   children: []
    // },
    // {
    //   path: '/example',
    //   name: 'example',
    //   component: exampleIndex,
    //   redirect: {name : 'list'},
    //   meta: {
    //     routerName: '实例 Example',
    //     icon: 'el-icon-star-off',
    //     showNavLayout: true
    //   },
    //   children: [
    //     {
    //       path: 'list',
    //       name: 'list',
    //       component: list,
    //       meta: {
    //         routerName: '列表页 List',
    //         keepAlive: true,
    //         showNavLayout: true
    //       },
    //     },
    //     {
    //       path: 'detail',
    //       name: 'detail',
    //       component: detail,
    //       meta: {
    //         hidden: true,
    //         showNavLayout: true,
    //         redirectName:  'list',
    //         keepAlive: true,
    //         detail: true,
    //       },
    //     },
    //     {
    //       path: 'charts',
    //       name: 'charts',
    //       component: charts,
    //       meta: {
    //         routerName: '图表页 Charts',
    //         showNavLayout: true,
    //       },
    //     },
    //   ]
    // },
    {
      path: '/login',
      name: 'login',
      routerName: "登录页",
      component: Login,
      meta: {
        showNavLayout: false
      }
    },
    {
      path: "",
      redirect: "/login",
      routerName: "重定向router",
      meta: {
        showNavLayout: false
      },
    },
  ]
})
